#include "output_task.h"

static TaskHandle_t               output_task_Handle = NULL;

// void app_valva_ctrl_special(const uint16_t valve_off,
//                             const uint16_t valve_on, 
//                             const uint16_t *pTog_off,
//                             const uint16_t *pTog_on,
//                             uint8_t *pIndex,
// 							uint8_t num)
// {
// 	valva_tick++;
// 	if(valva_tick >= valve_time) {
// 		valva_tick = 0;	
// 		if(valve_output_state == 0) {
//             valve_output_state = 1;
//             VALVA_OUTPUT_OPEN();
// 			if(*pIndex < num) {
// 				valve_time = pTog_off[*pIndex];
// 				*pIndex += 1;
// 			} else {
// 				valve_time = valve_off;
// 				*pIndex = 0;
// 			}
// 		} else {
//             valve_output_state = 0;
// 			VALVA_OUTPUT_CLOSE();
// 			if(*pIndex < num) {
// 				valve_time = pTog_on[*pIndex];
// 			} else {
// 				valve_time = valve_on;
// 			}   
// 		}
// 	} 
// }



typedef struct
{
    uint8_t a;
    uint8_t a2;
    uint8_t a1;/* data */
}tset1;


void valva_aerofluxus(void)
{
    VALVA_OUTPUT_OPEN();
    vTaskDelay(1200);
    VALVA_OUTPUT_CLOSE();
}


void output_task_task(void* args)
{
    uint32_t set_duty = 0;
    breast_output_msg_t packet = {NULL};
    uint16_t hold_tim = 0;
    static uint8_t gear = 0;
    uint8_t mode = 0;
    uint16_t start_tim = 0;
    uint16_t pump_tick = 0; 
    uint8_t pump_start_stage_count = 0;
    uint8_t pump_tim_stage_start = 0;
    uint32_t moto_duty_ctrl = 0;
    uint16_t valva_tick = 0;
    uint16_t valve_time = 0;
    uint8_t valve_output_state = 0;
    pump_state pump_state_t = PUMP_INIT;
    EventGroupHandle_t event = NULL;
    EventBits_t uxBits = 0;
    static uint8_t last_mode = 0;
    event = get_evemt_handle();
    DC_5V_EN();

    while(1)
    {
        app_get_ctrl_packet(&packet);
        uxBits = xEventGroupWaitBits(event,BREAST_PUMP_START_BIT,pdFALSE,pdTRUE,0);
        /*实时更新泵阀的工作时*/
        if(mode == KNEAD_MODE) 
        {   
            hold_tim = packet.pPump_tim_hold[gear] / 10;
            start_tim = 5;
            
        } 
        else 
        {
            hold_tim = packet.pPump_tim_hold[gear] / 10;
            start_tim = packet.pPump_tim_start[gear] / 10;
        }
        
        /*控制泵阀开始工*/
        if((uxBits & BREAST_PUMP_START_BIT) == BREAST_PUMP_START_BIT)
        {    
        /*控制阀工作*/    
            valva_tick++;
            if(valva_tick >= valve_time) 
            {
                valva_tick = 0;
                if(valve_output_state == VALVA_CTRL_OFF) 
                {
                    valve_output_state = VALVA_CTRL_ON;
                    VALVA_OUTPUT_CLOSE();
                    xEventGroupClearBits(event,VALVA_STATE_EVENT_BIT);
                    valve_time = packet.pValve_off[gear] / 10;
                    mode = packet.packet.mode_gether;
                    if (mode == KNEAD_MODE && last_mode == KNEAD_MODE)
                    {
                        
                        if (gear > packet.packet.knead_gear_para)
                        {
                            gear--;
                        }
                        else if (gear < packet.packet.knead_gear_para)
                        {
                            gear++;
                        }
                    }
                    else if(mode == SUCKLE_MODE && last_mode == SUCKLE_MODE)
                    {
                        
                        if (gear > packet.packet.suckle_gear_para)
                        {
                            gear--;
                        }
                        else if (gear < packet.packet.suckle_gear_para)
                        {
                            gear++;
                        }
                    }
                    else
                    {
                         if(mode == KNEAD_MODE) 
                        {   
                            last_mode = KNEAD_MODE;
                            gear = packet.packet.knead_gear_para;
                            if(gear >= NINTH_GEAR)
                                gear = NINTH_GEAR;
                            hold_tim = packet.pPump_tim_hold[gear] / 10;
                            start_tim = 5;
                        } 
                        else 
                        {
                            last_mode = SUCKLE_MODE;
                            gear = packet.packet.suckle_gear_para;
                            if(gear >= NINTH_GEAR)
                                gear = NINTH_GEAR;
                            hold_tim = packet.pPump_tim_hold[gear] / 10;
                            start_tim = packet.pPump_tim_start[gear] / 10;
                        }
                    }
                } 
                else 
                {
                    valve_output_state = VALVA_CTRL_OFF;
                    VALVA_OUTPUT_OPEN();
                    xEventGroupSetBits(event,VALVA_STATE_EVENT_BIT);
                    valve_time = packet.pValve_on[gear] / 10; 
                }
            } 
            /*控制泵阀工作*/    
            if(valve_output_state == VALVA_CTRL_ON)
            {
                switch(pump_state_t) 
                {
                /*泵工作初始化*/
                case PUMP_INIT:
                    {
                        moto_duty_ctrl = 0;
                        pump_tick++;
                        if(pump_tick >= start_tim)
                        {
                            pump_tick = 0;
                            if(mode == KNEAD_MODE)
                            {
                                if(gear > 3)
                                {
                                    pump_state_t = PUMP_START;
                                     pump_tim_stage_start = 4;
                                    moto_duty_ctrl = 0;
                                }
                                else
                                {
                                    pump_state_t = PUMP_HOLD;
                                }
                                
                            }
                            else
                            {
                                if((gear == 0) || (gear == 1))
                                {
                                    pump_state_t = PUMP_HOLD;
                                }
                                else if(gear > 5)
                                {
                                    pump_state_t = PUMP_START;
                                    pump_tim_stage_start = 4;
                                    moto_duty_ctrl = 0;
                                }
                                else
                                {
                                    pump_state_t = PUMP_START;
                                    pump_tim_stage_start = 4;
                                }
                            }
                        }
                        
                        break;
                    }
                case PUMP_START:  
                    {
                        
                        pump_tick++; 
                        moto_duty_ctrl = pump_start_stage_count * (packet.pPump_hold[gear]/7);
                        if(pump_tick >= pump_tim_stage_start)
                        {
                            pump_start_stage_count++;
                            pump_tim_stage_start = 3;//2
                            
                            pump_tick = 0;
                            if(pump_start_stage_count == 7)
                            {
                                pump_state_t = PUMP_HOLD;
                                
                            }
                        }
                        break;
                    }
                /*泵工作状*/    
                case PUMP_HOLD: 
                    {
                        pump_tick++;
                        moto_duty_ctrl = packet.pPump_hold[gear];
                        if(pump_tick >= hold_tim) 
                        {
                            pump_state_t = PUMP_END;
                            pump_tick = 0;
                        }
                        break;
                    }
                    
                case PUMP_END: 
                    {                
                        moto_duty_ctrl = 0;
                        break;

                    }
                }
                set_duty = (uint32_t)((TIMER2_HIGH_COUNT-1) * ((float)moto_duty_ctrl / 10000.0));
            }
            else 
            {
                set_duty = 0;
                pump_tick = 0;
                pump_start_stage_count = 0;
                pump_state_t = PUMP_INIT;

            }
        }
        else if((uxBits & BREAST_PUMP_PAUSE_BIT) == BREAST_PUMP_PAUSE_BIT)
        {
            set_duty = 0;
            pump_tick = 0;
            pump_start_stage_count = 0;
            pump_state_t = PUMP_INIT;
            mode = packet.packet.mode_gether;
            if(mode == KNEAD_MODE) 
            {   
                last_mode = KNEAD_MODE;
                gear = packet.packet.knead_gear_para;
                if(gear >= NINTH_GEAR)
                    gear = NINTH_GEAR;
                hold_tim = packet.pPump_tim_hold[gear] / 10;
                start_tim = 5;
            } 
            else 
            {
                last_mode = SUCKLE_MODE;
                gear = packet.packet.suckle_gear_para;
                if(gear >= NINTH_GEAR)
                    gear = NINTH_GEAR;
                hold_tim = packet.pPump_tim_hold[gear] / 10;
                start_tim = packet.pPump_tim_start[gear] / 10;
            }
        }
        else
        {
           set_duty = 0;
            pump_tick = 0;
            pump_start_stage_count = 0;
            pump_state_t = PUMP_INIT;
        }
        if((uxBits & BREAST_VALVE_AEROFLUXUS_BIT) == BREAST_VALVE_AEROFLUXUS_BIT)
        {
            
            VALVA_AEROFLUXUS_START();
            xEventGroupClearBits(event,BREAST_VALVE_AEROFLUXUS_BIT);
            xEventGroupSetBits(event,BREAST_PUMP_PAUSE_BIT);
        }
        timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, set_duty);
        vTaskDelay(10);
    }
}


void output_task_init(void)
{


    if(xTaskCreate(output_task_task,"output_task_task",128,NULL,configMAX_PRIORITIES-2,&output_task_Handle) == pdPASS)
    {
        asm("nop");
    //SEGGER_RTT_printf(0,"Create output_task sucess...\r\n");
    }
    
    
    // if(xTaskCreate(valva_task,"valva_task",128,NULL,4,&valva_task_Handle) == pdPASS)
    //     printf("Create valva_task sucess...\r\n");    
}

void output_task_deinit(void)
{
    vTaskDelete(output_task_Handle);
}